package com.ikangtai.shecare.curve.mpchart;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.al.DayUnitDSOutput;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class Ovulation {
    /**
     * BBTOvulationStart 里记录的是 dayUnitDSOutputsList 里所有dayUnitDSOutput为排卵期开始的下标位置position,
     * BBTOvulationEnd 里记录的是 dayUnitDSOutputsList 里所有dayUnitDSOutput为排卵期结束的下标位置position.
     */
    private static List<Integer> BBTOvulationStart = new ArrayList<>();
    private static List<Integer> BBTOvulationEnd = new ArrayList<>();
    private static List<Integer> BBTOvulationDay = new ArrayList<>();

    private static boolean inited = false;

    public static void init(){
        initBBTOvulationStartEndDayList();
        initBBTLineChartxValue2PeriodMap();
        inited = true;
    }

    private static void initBBTOvulationStartEndDayList(){
        BBTOvulationStart.clear();
        BBTOvulationEnd.clear();
        BBTOvulationDay.clear();

        for(int i = 0; i < App.dayUnitDSOutputsList.size(); i++){
//            AppLog.i("BBTLineChart dayUnitDSOutputsList date = " + DateUtil.getSimpleCircleCalendarTopDate(App.dayUnitDSOutputsList.get(i).date));
//            AppLog.i("BBTLineChart dayUnitDSOutputsList period = " + App.dayUnitDSOutputsList.get(i).getPeriod(App.status));

            float ovulationXAxisValue = BBTLineUtil.getXAxisValue(App.dayUnitDSOutputsList.get(i).date);

            if(isOvulationStart(i) && BBTLineUtil.isValidXAxisValue(ovulationXAxisValue) || isFirstXAxisValueOvulation(ovulationXAxisValue, i))
                BBTOvulationStart.add(i);

            if(isOvulationEnd(i) && BBTLineUtil.isValidXAxisValue(ovulationXAxisValue) || isFinalXAxisValueOvulation(ovulationXAxisValue, i))
                BBTOvulationEnd.add(i);
        }
    }

    private static boolean isOvulationStart(int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        if(position == 0 && isOvulationPeriod(period, position)) return true;
        if(position == 0 && !isOvulationPeriod(period, position)) return false;

        DayUnitDSOutput previousDayUnitDSOutput = App.dayUnitDSOutputsList.get(position - 1);
        String previousDayPeriod = previousDayUnitDSOutput.getPeriod(App.status);

        if(isOvulationPeriod(period, position) &&
                !isOvulationPeriod(previousDayPeriod, position - 1))
            return true;
        else
            return false;
    }

    private static boolean isOvulationEnd(int position){

        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        if(position == App.dayUnitDSOutputsList.size() - 1 && isOvulationPeriod(period, position)) return true;
        if(position == App.dayUnitDSOutputsList.size() - 1 && !isOvulationPeriod(period, position)) return false;

        DayUnitDSOutput NextDayUnitDSOutput = App.dayUnitDSOutputsList.get(position + 1);
        String nextDayPeriod = NextDayUnitDSOutput.getPeriod(App.status);

        if(isOvulationPeriod(period, position) &&
                !isOvulationPeriod(nextDayPeriod, position + 1))
            return true;
        else
            return false;
    }

    /**
     * 刚好曲线X轴的第一天为处于排卵期，但是在 dayUnitDSOutputsList 中是排卵期的中间位置，非起始位置
     * @param ovulationXAxisValue
     * @param position
     * @return
     */
    private static boolean isFirstXAxisValueOvulation(float ovulationXAxisValue, int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        return ovulationXAxisValue == BBTLineChart.MIN_XAXIS_DAY && isOvulationPeriod(period, position);
    }

    /**
     * 刚好曲线X轴的最后一天为处于排卵期，但是在 dayUnitDSOutputsList 中是排卵期的中间位置，非末位置
     * @param ovulationXAxisValue
     * @param position
     * @return
     */
    private static boolean isFinalXAxisValueOvulation(float ovulationXAxisValue, int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        return ovulationXAxisValue == BBTLineChart.MAX_XAXIS_DAY && isOvulationPeriod(period, position);
    }

    public static boolean isOvulationPeriod(String period, int position){
        if(period.equals(DayUnitDSOutput.PERIOD_PLQ))
            return true;
        else if(period.equals(DayUnitDSOutput.PERIOD_PLR)){
            BBTOvulationDay.add(position);
            return true;
        }
        else
            return false;
    }

    public static void initBBTLineChartxValue2PeriodMap(){
        for(int i = 0; i < BBTOvulationStart.size(); i++){
            int ovulationStartPoisition = BBTOvulationStart.get(i);
            int ovulationEndPoisition = BBTOvulationEnd.get(i);

            AppLog.i("BBTLineChart Ovulation background, i = " + i + ", ovulationStartPoisition = " + ovulationStartPoisition + ", ovulationEndPoisition = " + ovulationEndPoisition);

            initOvulationXValue2PeriodMap(ovulationStartPoisition, ovulationEndPoisition);
        }
    }

    private static void initOvulationXValue2PeriodMap(int ovulationStartPoisition, int ovulationEndPoisition){
        for(int ovulationPoistion = ovulationStartPoisition; ovulationPoistion <= ovulationEndPoisition; ovulationPoistion ++){
            DayUnitDSOutput ovulationDayUnitDSOutput = App.dayUnitDSOutputsList.get(ovulationPoistion);
            float ovulationXAxisValue = BBTLineUtil.getXAxisValue(ovulationDayUnitDSOutput.date);

            AppLog.i("BBTLineChart ovulationXAxisValue = " + DateUtil.getSimpleCircleCalendarTopDate(ovulationDayUnitDSOutput.date));

            BBTLineChart.bbtLineChartxValue2PeriodMap.put(ovulationXAxisValue, DayUnitDSOutput.PERIOD_PLQ);
        }
    }

    public static boolean isInOvulation(long daySeconds){
//        boolean inOvulation = false;
//
//        for(int i = 0; i < BBTOvulationStart.size(); i++) {
//            int ovulationStartPoisition = BBTOvulationStart.get(i);
//            DayUnitDSOutput ovulationStartDayUnitDSOutput = App.dayUnitDSOutputsList.get(ovulationStartPoisition);
//
//            int ovulationEndPoisition = BBTOvulationEnd.get(i);
//            DayUnitDSOutput ovulationEndDayUnitDSOutput = App.dayUnitDSOutputsList.get(ovulationEndPoisition);
//
//            if(daySeconds >= ovulationStartDayUnitDSOutput.date - 12 * 3600 && daySeconds <= ovulationEndDayUnitDSOutput.date + 12 * 3600){
//                inOvulation = true;
//                break;
//            }
//        }
//
//        return inOvulation;
        float xAxisValue = BBTLineUtil.getXAxisValue(daySeconds);
        return BBTLineChart.bbtLineChartxValue2PeriodMap.get(xAxisValue) == DayUnitDSOutput.PERIOD_PLQ;
    }


    public static boolean isOvulationDay(long daySeconds){
        boolean inOvulationDay = false;

        for(int i = 0; i < BBTOvulationDay.size(); i++) {
            int ovulationPoisition = BBTOvulationDay.get(i);
            DayUnitDSOutput ovulationDayUnitDSOutput = App.dayUnitDSOutputsList.get(ovulationPoisition);

            // daySeconds 是 00:00:00，而ovulationDayUnitDSOutput.date 是 12:00:00，因此用 >= && < (注意！不能用 <=)
            if(daySeconds >= ovulationDayUnitDSOutput.date - 12 * 3600 && daySeconds < ovulationDayUnitDSOutput.date + 12 * 3600){
                AppLog.e("BBTLineChart isOvulationDay! daySeconds = " + daySeconds + ", ovulationDayUnitDSOutput.date = " + ovulationDayUnitDSOutput.date);
                inOvulationDay = true;
                break;
            }
        }

        return inOvulationDay;
    }

    public static List<Integer> getBBTOvulationStartList(){
        if(!inited)
            init();

        return BBTOvulationStart;
    }

    public static List<Integer> getBBTOvulationEndList(){
        if(!inited)
            init();

        return BBTOvulationEnd;
    }
}
